import {
  addPrinter,
  getPrinterHint
} from '../../../utils/networkRequest.js'
import {
  errorMsg
} from '../../../utils/tool.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name: "",
    no: "",
    isList: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    getPrinterHint((r) => {
      if (r.code == 0) {
        self.setData({
          dataInfo: r.data,
          isList: options.isList ? options.isList:""
        })
      } else {
        errorMsg(r.message)
      }
    })
  },
  getName: function(e) {
    this.setData({
      name: e.detail.value
    })
  },
  getNo: function(e) {
    this.setData({
      no: e.detail.value
    })
  },
  addPrinter: function() {
    let {
      name,
      no,
      isList
    } = this.data;
    if (name == "") {
      errorMsg("请输入打印机名")
    } else if (no == "") {
      errorMsg("请输入打印机编号")
    } else {
      addPrinter(name, no, (r) => {
        if (r.code == 0) {
          //添加成功
          if (isList) {
            wx.navigateBack();
          } else {
            wx.redirectTo({
              url: '../printerlist/printerlist',
            })
          }

        } else {
          errorMsg(r.message);
        }
      })
    }

  }

})